package com.pinyougou.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.common.pojo.PageResult;
import com.pinyougou.pojo.Specification;
import com.pinyougou.pojo.SpecificationOption;
import com.pinyougou.service.SpecificationService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @date 16:32 2019/7/9
 */
@RestController
@RequestMapping("/specification")
public class SpecificationController {

    @Reference(timeout = 10000)
    private SpecificationService specificationService;

    /** 多条件分页查询 */
    @GetMapping("/findByPage")
    public PageResult findByPage(Specification specification,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer rows) {
       /* // Get请求转码
        try {
            if(StringUtils.isNoneBlank(specification.getSpecName())) {
                specification.setSpecName(new String(specification.getSpecName().getBytes("ISO8859-1"),"UTF-8"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }*/
        return specificationService.findByPage(specification,page,rows);
    }

    /** 保存规格 */
    @PostMapping("/save")
    public boolean save(@RequestBody Specification specification){
        try {
            specificationService.save(specification);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    /** 更新规格 */
    @PostMapping("/update")
    public boolean update(@RequestBody Specification specification){
        try {
            specificationService.update(specification);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    /** 查询规格选项 */
    @GetMapping("/findSpecOptionsBySpecId")
    public List<SpecificationOption> findSpecOptionsBySpecId(@RequestParam("id") Long specId){
            return specificationService.findSpecOptionsBySpecId(specId);
    }
    /** 删除规格及规格选项 */
    @GetMapping("/delete")
    public boolean delete(Long[] ids){
        try {
            specificationService.deleteAll(ids);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    /** 查询全部规格(id和name)*/
    @GetMapping("/findSpecList")
    List<Map<String,Object>> findSpecList(){
        return specificationService.findAllByIdAndName();
    }

}